import { createStore } from 'vuex'
import { login_api } from '@/api/login_api'
import { getUserInfo } from '@/api/user_api'
import createPersistedstate from 'vuex-persistedstate'
export default createStore({
  state: {
    //用户信息
    userInfo: {
      roles: <String[]>[], //权限
      avatar: '', //头像
      userId: "", //用户id
      status: false, //登录状态
      username: '' //用户名
    },
    //token
    access_token: '',
  },
  getters: {
    uid: state => state.userInfo.userId,
    uname: state => state.userInfo.username
  },
  mutations: {
    setTOKEN(state, TOKEN) {
      state.access_token = TOKEN
    },
    setuserIfo(state,res){
      state.userInfo=res
    }
  },

  actions: {
    // 登录
    action_login({ commit }, user: any) {

      return login_api(user).then((res: any) => {
        console.log(res, this);
        commit("setTOKEN", res.access_token)
        // console.log(state);

        return res

      })
    },
    //获取用户信息
    get_user_api({ commit }) {
      return getUserInfo().then((res: any) => {
        // console.log(res);
        commit("setuserIfo", res)
        // state.userInfo = res
        return res;
      })
    }
  },
  modules: {
  },
  plugins: [
    createPersistedstate()
  ]
})
